package com.centaline.androidsalesblog.widgets.dropdown;

import android.support.v7.widget.AppCompatTextView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.model.businissobject.DropBo;

import java.util.ArrayList;

/**
 * 下拉菜单-文本
 * Created by guilin on 16/1/13.
 */
public class DropTextAdapter extends BaseAdapter {

    private ArrayList<DropBo> arrayList;
    private int selectedPos;//选中位置

    public DropTextAdapter() {
        this(new ArrayList<DropBo>());
    }

    public DropTextAdapter(ArrayList<DropBo> arrayList) {
        this.arrayList = arrayList;
    }

    public void select(int position) {
        selectedPos = position;
        notifyDataSetChanged();
    }

    public void setArrayList(ArrayList<DropBo> list) {
        arrayList.clear();
        arrayList.addAll(list);
        notifyDataSetChanged();
    }

    public void setSelectedPos(int position) {
        selectedPos = position;
    }

    public ArrayList<DropBo> getArrayList() {
        return arrayList;
    }

    public int getSelectedPos() {
        return selectedPos;
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DropVH dropVH;
        if (convertView == null) {
            dropVH = new DropVH();
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_drop_text, parent, false);
            dropVH.fl_parent = (FrameLayout) convertView.findViewById(R.id.fl_parent);
            dropVH.atv_name = (AppCompatTextView) convertView.findViewById(R.id.atv_name);
            convertView.setTag(dropVH);
        } else {
            dropVH = (DropVH) convertView.getTag();
        }
        dropVH.atv_name.setText(arrayList.get(position).getText());
        final boolean selected = selectedPos == position;
        dropVH.fl_parent.setBackgroundResource(selected ?
                R.drawable.ic_table_selected : R.drawable.ic_table_normal);
        dropVH.atv_name.setTextColor(selected ? 0xFFF13E06 : 0xFF333333);
        return convertView;
    }

    class DropVH {
        public FrameLayout fl_parent;
        public AppCompatTextView atv_name;
    }
}
